#include "ShapeContainer.h"

//d'tor
ShapeContainer::~ShapeContainer()
{
	for(list<Shape*>::iterator itr = m_shape_cont.begin(); itr != m_shape_cont.end(); ++itr)
	{
		//delete from list
	}
}

void ShapeContainer::drawList(bool withHiding)
{
	if(withHiding)
	{
		for(list<Shape*>::iterator itr = m_shape_cont.begin(); itr != m_shape_cont.end(); ++itr)
			(*itr)->drawWithHiding();
	}
	else
	{
		for(list<Shape*>::iterator itr = m_shape_cont.begin(); itr != m_shape_cont.end(); ++itr)
			(*itr)->draw();
	}
}


list<Shape*>::iterator ShapeContainer::findContainingShape(int x, int y)
{
	list<Shape*>::iterator itr = m_shape_cont.begin();
	for( ; itr != m_shape_cont.end(); ++itr)
	{
		if( (*itr)->isInShape(x,y))
			return itr;
	}
	return itr;
}

void ShapeContainer::addShapeToList(Shape *shape)
{
	shape->setShape();
	m_shape_cont.push_back(shape);
}

void ShapeContainer::bringToFront( list<Shape*>::iterator itr)
{
	Shape *tmp = *itr;
	m_shape_cont.erase(itr);
	m_shape_cont.push_front(tmp);
}

void ShapeContainer::moveToBack(list<Shape*>::iterator itr)
{
	Shape *tmp = *itr;
	m_shape_cont.erase(itr);
	m_shape_cont.push_back(tmp);
}

void ShapeContainer::copyContainer(ShapeContainer &other)
{
	for( list<Shape*>::iterator itr = other.getBeginningOfList(); itr != other.getEndOfList(); ++itr)
		m_shape_cont.push_back( (*itr)->copyShape(*itr));
}

void ShapeContainer::calcMove(bool withTransfer, ShapeContainer &work_cont)
{
	for(list<Shape*>::iterator itr = work_cont.getBeginningOfList(); itr != work_cont.getEndOfList(); ++itr)
	{
		if(!didIMove(itr)) //if shape does not move
		{
			if( (*itr)->getShiftX() != 0)
				(*itr)->setShiftXTmp( (*itr)->getShiftXTmp() + (*itr)->getShiftX());
			if( (*itr)->getShiftY() != 0)
				(*itr)->setShiftYTmp( (*itr)->getShiftYTmp() + (*itr)->getShiftY());
			continue;
		}
		(*itr)->calcShapeMove(*this,itr,withTransfer);
	}
}

void ShapeContainer::deleteShape(list<Shape*>::iterator itr)
{
	delete *itr;
	m_shape_cont.erase(itr);
}